package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliossUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
public class UploadFileController {

    @Autowired
    private AliossUtil aliossUtil;

    @PostMapping
    public Result uploadFile(MultipartFile file) throws IOException {

        String originalFilename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String newFileName = uuid + "_" + originalFilename;

        String url = aliossUtil.uploadFile(newFileName, file.getInputStream());

        return Result.success("上传成功",url);


    }

}
